package com.hsf.springboot.elasticsearch.controller;


import com.hsf.springboot.elasticsearch.domain.CommonResult;
import com.hsf.springboot.elasticsearch.domain.Person;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;


@RestController
@RequestMapping("/elastic")
public class TestController {

   private static Logger logger = LoggerFactory.getLogger(TestController.class);

    @Autowired
    private RestHighLevelClient restHighLevelClient;



    @PostMapping("/person")
    public CommonResult<IndexResponse> addPerson(@RequestBody Person person){
        logger.info("id={},name={},age={}",person.getId(),person.getName(),person.getAge());
        try {
            XContentBuilder xContentBuilder = JsonXContent.contentBuilder();
            xContentBuilder.startObject()
                    .field("name",person.getName())
                    .field("age",person.getAge())
                    .endObject().humanReadable(true);
            IndexRequest indexRequest = new IndexRequest();
            indexRequest.id(person.getId()).source(xContentBuilder);
            indexRequest.index("person").type("_doc");
            IndexResponse response = restHighLevelClient.index(indexRequest,RequestOptions.DEFAULT);
            return  CommonResult.success(response);
        } catch (IOException e) {
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }

    }

    @GetMapping("/person")
    public CommonResult<GetResponse> queryPerson(@RequestParam("id") String id){
        logger.info("id={}",id);
        GetRequest getRequest = new GetRequest();
        getRequest.id(id);
        getRequest.index("person");
        getRequest.type("_doc");
        try {
            GetResponse response = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
            return CommonResult.success(response);
        } catch (IOException e) {
            logger.error("get person failed",e);
            return CommonResult.failed(e.getMessage());
        }

    }

    @PutMapping("/person")
    public CommonResult<UpdateResponse> updateUser(@RequestBody Person person){
        logger.info("id={},name={},age={}",person.getId(),person.getName(),person.getAge());
        try {
            XContentBuilder xContentBuilder = JsonXContent.contentBuilder();
            xContentBuilder.startObject()
                    .field("name",person.getName())
                    .field("age",person.getAge())
                    .endObject().humanReadable(true);
            UpdateRequest updateRequest = new UpdateRequest();
            updateRequest.index("person");
            updateRequest.id(person.getId());
            updateRequest.type("_doc");
            updateRequest.doc(xContentBuilder);
            UpdateResponse response = restHighLevelClient.update(updateRequest,RequestOptions.DEFAULT);
            return CommonResult.success(response);
        } catch (IOException e) {
            logger.error("update person failed",e);
            return CommonResult.failed(e.getMessage());
        }
    }

    @DeleteMapping("/person/{id}")
    public CommonResult<DeleteResponse> deletePerson(@PathVariable("id") String id){
        logger.info("id={}",id);
        DeleteRequest deleteRequest = new DeleteRequest();
        deleteRequest.index("person");
        deleteRequest.id(id);
        deleteRequest.type("_doc");
        try {
            DeleteResponse response = restHighLevelClient.delete(deleteRequest,RequestOptions.DEFAULT);
            return CommonResult.success(response);
        } catch (IOException e) {
            logger.error("delete person failed",e);
            return CommonResult.failed(e.getMessage());
        }

   }


}
